package io.nutz.nutzsite.module.exam.controller;

import io.nutz.nutzsite.common.base.Result;
import io.nutz.nutzsite.module.exam.models.Paperdef;
import io.nutz.nutzsite.module.exam.models.Papertpl;
import io.nutz.nutzsite.module.exam.models.Qdb;
import io.nutz.nutzsite.module.exam.models.Qtype;
import io.nutz.nutzsite.module.exam.services.PaperdefService;
import io.nutz.nutzsite.module.exam.services.PapertplService;
import io.nutz.nutzsite.module.exam.services.QdbService;
import io.nutz.nutzsite.module.exam.services.QtypeService;
import io.nutz.nutzsite.module.exam.services.QuestionService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;
import org.nutz.plugins.slog.annotation.Slog;
/**
 * 试卷模板 信息操作处理
 * 
 * @author lrw
 * @date 2020-04-08
 */
@IocBean
@At("/exam/papertpl")
public class PapertplController {
	private static final Log log = Logs.get();

	@Inject
	private PapertplService papertplService;
	@Inject
	private QtypeService qtypeService;
	@Inject
	private QuestionService questionService;
	@Inject
	private PaperdefService paperdefService;
	@Inject
	private QdbService qdbService;
	
	@RequiresPermissions("exam:papertpl:view")
	@At("/?")
	@Ok("th:/exam/papertpl/papertpl.html")
	public void index(String id,HttpServletRequest req) {
		//req.setAttribute("ksid", id);
		Paperdef paperdef=paperdefService.fetch(id);
		req.setAttribute("paperdef", paperdef);
	}

	/**
	 * 查询试卷模板列表
	 */
	@RequiresPermissions("exam:papertpl:list")
	@At
	@Ok("json")
	public Object list(@Param("pageNum")int pageNum,
					   @Param("pageSize")int pageSize,
					   @Param("ksid") String ksid,
					   @Param("orderByColumn") String orderByColumn,
					   @Param("isAsc") String isAsc,
					   HttpServletRequest req) {
		Cnd cnd = Cnd.NEW();
		if (!Strings.isBlank(ksid)){
			cnd.and("ksid", "=", ksid);
		}else cnd.and("ksid", "=", "0");//返回空

		return papertplService.tableList(pageNum,pageSize,cnd,orderByColumn,isAsc,"qdb|qdbkb|qtype");
	}

	/**
	 * 新增试卷模板
	 */
	@At("/add/?")
	@Ok("th:/exam/papertpl/add.html")
	public void add(String id, HttpServletRequest req) {
		req.setAttribute("ksid", id);
		List<Qtype> list=qtypeService.query();
		req.setAttribute("qtypes", list);
		Qdb qdb=new Qdb();
		qdb.setId(1);
		qdb.setName("选择题库/知识点");
		req.setAttribute("qdb",qdb);
	}

	/**
	 * 新增保存试卷模板
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:papertpl:add")
	@Slog(tag="试卷模板", after="新增保存试卷模板 id=${args[0].id}")
	public Object addDo(@Param("..") Papertpl papertpl,HttpServletRequest req) {
		try {
			papertplService.insert(papertpl);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}
	@POST
	@Ok("json")
	public Object getQnumofDB(@Param("qdbid") int qdbid,
			@Param("kbid") Integer kbid,
			@Param("qtid") int qtid,
			@Param("qteasy") int qteasy,
			HttpServletRequest req) {
		try {
			Cnd cnd = Cnd.NEW();
			cnd.and("qdbid", "=", qdbid);
			if(kbid!=null) cnd.and("kbid", "=", kbid);
			cnd.and("qtid", "=", qtid);
			cnd.and("status","=",1);//开放的，可用的题
			if(qteasy>0) cnd.and("qteasy", "=", qteasy);
			return questionService.tableList(1, 2000, cnd);
		} catch (Exception e) {
			return null;
		}
	}
	/**
	 * 修改试卷模板
	 */
	@At("/edit/?")
	@Ok("th:/exam/papertpl/edit.html")
	public void edit(int id, HttpServletRequest req) {
		Papertpl papertpl = papertplService.fetch(id);
		req.setAttribute("papertpl",papertpl);
		
		/*req.setAttribute("ksid", papertpl.getKsid());*/
		List<Qtype> list=qtypeService.query();
		req.setAttribute("qtypes", list);
		
		Qdb qdb=qdbService.fetch(papertpl.getKbid());
		req.setAttribute("qdb",qdb);
		
	}

    /**
     * 详情试卷模板
     */
    @At("/detail/?")
    @Ok("th:/exam/papertpl/detail.html")
    public void detail(String id, HttpServletRequest req) {
		Papertpl papertpl = papertplService.fetch(id);
        req.setAttribute("papertpl",papertpl);
    }

    /**
	 * 修改保存试卷模板
	 */
	@At
	@POST
	@Ok("json")
	@RequiresPermissions("exam:papertpl:edit")
	@Slog(tag="试卷模板", after="修改保存试卷模板")
	public Object editDo(@Param("..") Papertpl papertpl,HttpServletRequest req) {
		try {
			if(Lang.isNotEmpty(papertpl)){
				papertplService.update(papertpl);
			}
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

	/**
	 * 删除试卷模板
	 */
	@At("/remove")
	@Ok("json")
	@RequiresPermissions("exam:papertpl:remove")
	@Slog(tag ="试卷模板", after= "删除试卷模板:${array2str(args[0])}")
	public Object remove(@Param("ids")String[] ids, HttpServletRequest req) {
		try {
			papertplService.delete(ids);
			return Result.success("system.success");
		} catch (Exception e) {
			return Result.error("system.error");
		}
	}

}
